package server.display;

import com.esotericsoftware.minlog.Log;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import server.ServerManager;
import util.Client;
import util.Screenshot;

public class ClientRemoteDesktop extends javax.swing.JDialog {

    private Client client;
    private ServerManager server;
    
    public ClientRemoteDesktop(java.awt.Frame parent, boolean modal) {
        this(parent, modal, null);
    }

    public ClientRemoteDesktop(java.awt.Frame parent, boolean modal, final Client client) {
        super(parent, modal);
        
        this.server = (ServerManager) parent;
        this.client = client;
        
        initComponents();
        
        this.setTitle(String.format("Client Remote Desktop - %s", client));
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent we) {
                net.packet.RemoteDesktop packet = new net.packet.RemoteDesktop();
                
                packet.start = false;
                
                server.sendUDP(client.getConnection(), packet);
            }
        });

        pnCaptureArea.setClientRemoteDesktop(this);
    }

    public void updateImage(byte[] image) {
        try {
            pnCaptureArea.updateImage(Screenshot.convertToBufferedImage(image));
        } catch (IOException ex) {
            server.writeToLog(Log.LEVEL_ERROR, "[RemoteDesktop] Failed to update image", ex);
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnCaptureArea = new server.util.panel.ClientRemoteDesktopPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        javax.swing.GroupLayout pnCaptureAreaLayout = new javax.swing.GroupLayout(pnCaptureArea);
        pnCaptureArea.setLayout(pnCaptureAreaLayout);
        pnCaptureAreaLayout.setHorizontalGroup(
            pnCaptureAreaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 490, Short.MAX_VALUE)
        );
        pnCaptureAreaLayout.setVerticalGroup(
            pnCaptureAreaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 330, Short.MAX_VALUE)
        );

        getContentPane().add(pnCaptureArea, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public static void main(String args[]) {
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ClientRemoteDesktop.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ClientRemoteDesktop.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ClientRemoteDesktop.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ClientRemoteDesktop.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                ClientRemoteDesktop dialog = new ClientRemoteDesktop(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private server.util.panel.ClientRemoteDesktopPanel pnCaptureArea;
    // End of variables declaration//GEN-END:variables

}
